package cn.volunteer.points.system.service.impl;

import cn.volunteer.points.system.handle.ServiceErrorCode;
import cn.volunteer.points.system.handle.ServiceException;
import cn.volunteer.points.system.mapper.UserMapper;
import cn.volunteer.points.system.pojo.entity.Password;
import cn.volunteer.points.system.pojo.entity.User;
import cn.volunteer.points.system.pojo.qo.LoginQO;
import cn.volunteer.points.system.pojo.qo.RegisterQO;
import cn.volunteer.points.system.pojo.vo.UserVO;
import cn.volunteer.points.system.service.UserService;
import cn.volunteer.points.system.util.JWTUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
 * @description: 请输入描述信息
 * @author:
 * @create: 创建时间：2021-04-16
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper ;


    @Override
    public UserVO login(LoginQO user) {

        String token  = null ;
        UserVO userVO = new UserVO();
        User result = userMapper.findUserBy(user);
        if (ObjectUtils.isEmpty(result)) {
            throw new ServiceException(ServiceErrorCode.USER_NO_EXEIT);
        }
        if (!result.getPassword().equals(user.getPassword())) {
            throw new ServiceException(ServiceErrorCode.PASSWORD_ERROR);
        }
        token = JWTUtils.getToken(result);
        BeanUtils.copyProperties(result,userVO);
        userVO.setToken(token);
        return userVO;
    }

    @Override
    public String addUser(RegisterQO user) {

        String phone = user.getPhone();

        User byPhone = userMapper.findByPhone(phone);

        if (!ObjectUtils.isEmpty(byPhone)) {
            return "号码已存在！！！，请勿重复使用";
        }

        String userName = user.getUserName();

        User byUserName = userMapper.findByUserName(userName);

        if (!ObjectUtils.isEmpty(byUserName)) {
            return "用户名已存在！！！，请勿重复使用";
        }
        userMapper.addUser(user);

        return "注册成功！！！";
    }

    @Override
    public void delUser(Integer userId) {
        userMapper.delUser(userId);
    }

    @Override
    public String updateUser(User user) {


        userMapper.updateUser(user);

        return "修改用户信息成功！！！";

    }

    @Override
    public String updateUserAdmin(User user) {

        userMapper.updateUserAdmin(user);

        return "修改用户信息成功！！！";
    }

    @Override
    public UserVO findById(Integer userId) {

        return userMapper.findById(userId);
    }

    @Override
    public List<UserVO> userLisr(String searchValue) {
        return userMapper.findUserList(searchValue);
    }

    @Override
    public JSONObject updatePassword(Password password) {
        String oldPassword = password.getOldPassword();

        User userByUid = userMapper.findUserByUid(password.getUserId());

        JSONObject jsonObject = new JSONObject();

        if (userByUid.getPassword().equals(oldPassword)) {
            userMapper.updatePassword(password);
            jsonObject.put("code",0);
            jsonObject.put("msg","修改密码成功！");
           return jsonObject ;
        }

        jsonObject.put("code",-1);
        jsonObject.put("msg","旧密码不一致！");
        return jsonObject ;

    }


}
